#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <cJSON.h>
#include "ai_client.h"
#include "context.h"
#include "history.h"
#include <pthread.h>


#define SERVER_IP "192.168.172.11"
#define PORT 11434

char response_data[1024 * 10];

static void content_printf(char *content)
{
    // printf("%s", content);
    strcat(response_data, content);
}

// gcc test_main.c ai_client.c context.c history.c -lcjson

int _main(int argc, char const *argv[])
{

    // 测试整体功能
    QM_P queue = init_queue_manage();
    history_init();

    char p[3][1024];

    // 聊天
    int client_fd = client_init();

    // 一次完整的提问流程
    // ========================================
    char title[100] = "1";
    char question[100] = "你好";

    en_queue(queue, question);

    get_context(queue, p);

    memset(response_data, 0, sizeof(response_data)); // 每次提问前清空之前回答的缓冲区
    client_chat_context(client_fd, p, 1, content_printf);
    printf("%s\n", response_data);

    history_set(title, question, response_data);
    // ========================================


    // ========================================
    memset(question, 0, sizeof(question));
    strcpy(question, "你是谁");

    en_queue(queue, question);

    get_context(queue, p);

    memset(response_data, 0, sizeof(response_data)); // 每次提问前清空之前回答的缓冲区
    client_chat_context(client_fd, p, 2, content_printf);
    printf("%s\n", response_data);
    history_set(title, question, response_data); // 记录历史记录
    // ========================================



    // ========================================
    memset(question, 0, sizeof(question));

    strcpy(question, "你能做什么？");
    en_queue(queue, question);

    get_context(queue, p);

    memset(response_data, 0, sizeof(response_data)); // 每次提问前清空之前回答的缓冲区
    client_chat_context(client_fd, p, 2, content_printf);
    client_chat_context(client_fd, p, MAX_CONTEXT, content_printf);
    history_set(title, question, response_data); // 记录历史记录
    // ========================================



    client_close(client_fd);

    history_close();

    return 0;
}
